package com.jstyle.jclife.utils;

import android.app.Activity;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.Uri;

import com.google.android.gms.maps.model.LatLng;
import com.jstyle.jclife.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class Utils {
    // 两次点击按钮之间的点击间隔不能少于600毫秒
    private static final int MIN_CLICK_DELAY_TIME = 600;
    private static final int MIN_CLICK_DELAY_TIMEer = 1000;
    private static long lastClickTime;

    public static boolean isFastClick() {
        boolean flag = true;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = false;
        }
        lastClickTime = curClickTime;
        return flag;
    }
    public static boolean isFastClicktwo() {
        boolean flag = true;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIMEer) {
            flag = false;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    public static boolean checkNetWork(Context context) {
        boolean flag = false;
        ConnectivityManager cwjManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cwjManager.getActiveNetworkInfo() != null)
            flag = cwjManager.getActiveNetworkInfo().isAvailable();
        return flag;
    }


    public static final boolean isOPen(final Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }


    public static final void openGPS(Context context) {
        Intent GPSIntent = new Intent();
        GPSIntent.setClassName("com.android.settings",
                "com.android.settings.widget.SettingsAppWidgetProvider");
        GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
        GPSIntent.setData(Uri.parse("custom:3"));
        try {
            PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }


    public static boolean isZh(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        if (language.endsWith("zh"))
            return true;
        else
            return false;
    }


    public static float getGoogleDistance(LatLng start, LatLng end) {
        double lat1 = (Math.PI / 180) * start.latitude;
        double lat2 = (Math.PI / 180) * end.latitude;
        double lon1 = (Math.PI / 180) * start.longitude;
        double lon2 = (Math.PI / 180) * end.longitude;
        double R = 6371;//地球半径
        double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2)
                * Math.cos(lon2 - lon1))
                * R;
        return (float) d * 1000;
    }

    public static ProgressDialog progressDialog;

    public static void showProgressDialog(Activity activity, String message) {
        if (null == activity) {
            return;
        }
        if (activity.isDestroyed()) return;
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(activity, R.style.appalertdialog);
        }
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        progressDialog.setMessage(message);
        if (!activity.isFinishing()) {
            try {
                progressDialog.show();
            } catch (Exception E) {
                E.printStackTrace();
            }
        }
    }


    public static void disMissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    //判断是否在台湾
    public static boolean isTAiwan( com.amap.api.maps.model.LatLng var1) {
        List<com.amap.api.maps.model.LatLng> var0=new ArrayList<>();
        var0.add(new com.amap.api.maps.model.LatLng(22.210757,113.546513));
        var0.add(new com.amap.api.maps.model.LatLng(22.220009,113.556376));
        var0.add(new com.amap.api.maps.model.LatLng(22.219557,113.560131));
        var0.add(new com.amap.api.maps.model.LatLng(22.219724,113.562808));
        var0.add(new com.amap.api.maps.model.LatLng(22.219724,113.562808));
        var0.add(new com.amap.api.maps.model.LatLng(22.216864,113.56712));
        var0.add(new com.amap.api.maps.model.LatLng(22.167888,113.607436));
        var0.add(new com.amap.api.maps.model.LatLng(22.137694,113.613472));
        var0.add(new com.amap.api.maps.model.LatLng(22.137694,113.613472));
        var0.add(new com.amap.api.maps.model.LatLng(22.11486,113.587457));
        var0.add(new com.amap.api.maps.model.LatLng(22.11178,113.567695));
        var0.add(new com.amap.api.maps.model.LatLng(22.114726,113.560077));
        var0.add(new com.amap.api.maps.model.LatLng(22.141243,113.561371));
        var0.add(new com.amap.api.maps.model.LatLng(22.141243,113.561371));
        var0.add(new com.amap.api.maps.model.LatLng(22.153026,113.558999));
        var0.add(new com.amap.api.maps.model.LatLng(22.162064,113.55016));
        var0.add(new com.amap.api.maps.model.LatLng(22.162064,113.55016));
        var0.add(new com.amap.api.maps.model.LatLng(22.185693,113.537943));
        var0.add(new com.amap.api.maps.model.LatLng(22.209252,113.548076));
        if(var0 != null && var0.size() != 0 && var1 != null) {
            int var2;
            for(var2 = 0; var2 < var0.size(); ++var2) {
                if(var1.longitude == ((com.amap.api.maps.model.LatLng)var0.get(var2)).longitude && var1.latitude == ((com.amap.api.maps.model.LatLng)var0.get(var2)).latitude) {
                    return true;
                }
            }
            var2 = 0;
            boolean var3 = false;
            com.amap.api.maps.model.LatLng var4 = null;
            com.amap.api.maps.model.LatLng var5 = null;
            double var6 = 0.0D;
            int var8 = var0.size();
            for(int var9 = 0; var9 < var8; ++var9) {
                var4 = (com.amap.api.maps.model.LatLng)var0.get(var9);
                var5 = (com.amap.api.maps.model.LatLng)var0.get((var9 + 1) % var8);
                if(var4.latitude != var5.latitude && var1.latitude >= Math.min(var4.latitude, var5.latitude) && var1.latitude <= Math.max(var4.latitude, var5.latitude)) {
                    var6 = (var1.latitude - var4.latitude) * (var5.longitude - var4.longitude) / (var5.latitude - var4.latitude) + var4.longitude;
                    if(var6 == var1.longitude) {
                        return true;
                    }
                    if(var6 < var1.longitude) {
                        ++var2;
                    }
                }
            }
            return var2 % 2 == 1;
        } else {
            return false;
        }
    }

    /**
     * 是否在香港
     * @param var1
     * @return
     */
    public static boolean isXianggan( com.amap.api.maps.model.LatLng var1) {
        List<com.amap.api.maps.model.LatLng> var0=new ArrayList<>();
        var0.add(new com.amap.api.maps.model.LatLng(22.445883,113.929154));
        var0.add(new com.amap.api.maps.model.LatLng(22.470079,113.964213));
        var0.add(new com.amap.api.maps.model.LatLng(22.485787,113.981478));
        var0.add(new com.amap.api.maps.model.LatLng(22.502390,114.018740));
        var0.add(new com.amap.api.maps.model.LatLng(22.504206,114.033034));
        var0.add(new com.amap.api.maps.model.LatLng(22.501687,114.049640));
        var0.add(new com.amap.api.maps.model.LatLng(22.501153,114.055955));
        var0.add(new com.amap.api.maps.model.LatLng(22.504341,114.059980));
        var0.add(new com.amap.api.maps.model.LatLng(22.512334,114.062863));
        var0.add(new com.amap.api.maps.model.LatLng(22.514971,114.066319));
        var0.add(new com.amap.api.maps.model.LatLng(22.514369,114.071502));
        var0.add(new com.amap.api.maps.model.LatLng(22.505164,114.078990));
        var0.add(new com.amap.api.maps.model.LatLng(22.501926,114.080718));
        var0.add(new com.amap.api.maps.model.LatLng(22.502735,114.095725));
        var0.add(new com.amap.api.maps.model.LatLng(22.502586,114.114177));
        var0.add(new com.amap.api.maps.model.LatLng(22.496206,114.122229));
        var0.add(new com.amap.api.maps.model.LatLng(22.492133,114.137170));
        var0.add(new com.amap.api.maps.model.LatLng(22.488512,114.143485));
        var0.add(new com.amap.api.maps.model.LatLng(22.484873,114.148651));
        var0.add(new com.amap.api.maps.model.LatLng(22.482833,114.153820));
        var0.add(new com.amap.api.maps.model.LatLng(22.474272,114.153244));
        var0.add(new com.amap.api.maps.model.LatLng(22.464612,114.150949));
        var0.add(new com.amap.api.maps.model.LatLng(22.450247,114.154407));
        var0.add(new com.amap.api.maps.model.LatLng(22.441265,114.160740));
        var0.add(new com.amap.api.maps.model.LatLng(22.442974,114.171675));
        var0.add(new com.amap.api.maps.model.LatLng(22.445657,114.178590));
        var0.add(new com.amap.api.maps.model.LatLng(22.436448,114.194191));
        var0.add(new com.amap.api.maps.model.LatLng(22.426537,114.212700));
        var0.add(new com.amap.api.maps.model.LatLng(22.531650,114.224823));
        var0.add(new com.amap.api.maps.model.LatLng(22.548767,114.247829));
        var0.add(new com.amap.api.maps.model.LatLng(22.555107,114.278195));
        var0.add(new com.amap.api.maps.model.LatLng(22.568399,114.312063));
        var0.add(new com.amap.api.maps.model.LatLng(22.589030,114.322414));
        var0.add(new com.amap.api.maps.model.LatLng(22.581747,114.422362));
        var0.add(new com.amap.api.maps.model.LatLng(22.541161,114.468636));
        var0.add(new com.amap.api.maps.model.LatLng(22.512846,114.472671));
        var0.add(new com.amap.api.maps.model.LatLng(22.462092,114.476686));
        var0.add(new com.amap.api.maps.model.LatLng(22.419542,114.494002));
        var0.add(new com.amap.api.maps.model.LatLng(22.363740,114.464569));
        var0.add(new com.amap.api.maps.model.LatLng(22.254018,114.425275));
        var0.add(new com.amap.api.maps.model.LatLng(22.173216,114.374778));
        var0.add(new com.amap.api.maps.model.LatLng(22.132902,114.350657));
        var0.add(new com.amap.api.maps.model.LatLng(22.117578,114.248971));
        var0.add(new com.amap.api.maps.model.LatLng(22.115810,114.128565));
        var0.add(new com.amap.api.maps.model.LatLng(22.155008,113.999891));
        var0.add(new com.amap.api.maps.model.LatLng(22.198143,113.940663));
        var0.add(new com.amap.api.maps.model.LatLng(22.197552,113.884447));
        var0.add(new com.amap.api.maps.model.LatLng(22.192136,113.829611));
        var0.add(new com.amap.api.maps.model.LatLng(22.220545,113.807664));
        var0.add(new com.amap.api.maps.model.LatLng(22.249156,113.787547));
        var0.add(new com.amap.api.maps.model.LatLng(22.319915,113.841737));
        var0.add(new com.amap.api.maps.model.LatLng(22.361818,113.873481));
        var0.add(new com.amap.api.maps.model.LatLng(22.445883,113.929154));
        var0.add(new com.amap.api.maps.model.LatLng(22.445883,113.929154));
        if(var0 != null && var0.size() != 0 && var1 != null) {
            int var2;
            for(var2 = 0; var2 < var0.size(); ++var2) {
                if(var1.longitude == ((com.amap.api.maps.model.LatLng)var0.get(var2)).longitude && var1.latitude == ((com.amap.api.maps.model.LatLng)var0.get(var2)).latitude) {
                    return true;
                }
            }
            var2 = 0;
            boolean var3 = false;
            com.amap.api.maps.model.LatLng var4 = null;
            com.amap.api.maps.model.LatLng var5 = null;
            double var6 = 0.0D;
            int var8 = var0.size();
            for(int var9 = 0; var9 < var8; ++var9) {
                var4 = (com.amap.api.maps.model.LatLng)var0.get(var9);
                var5 = (com.amap.api.maps.model.LatLng)var0.get((var9 + 1) % var8);
                if(var4.latitude != var5.latitude && var1.latitude >= Math.min(var4.latitude, var5.latitude) && var1.latitude <= Math.max(var4.latitude, var5.latitude)) {
                    var6 = (var1.latitude - var4.latitude) * (var5.longitude - var4.longitude) / (var5.latitude - var4.latitude) + var4.longitude;
                    if(var6 == var1.longitude) {
                        return true;
                    }
                    if(var6 < var1.longitude) {
                        ++var2;
                    }
                }
            }
            return var2 % 2 == 1;
        } else {
            return false;
        }
    }




    /**
     * 调换集合中两个指定位置的元素, 若两个元素位置中间还有其他元素，需要实现中间元素的前移或后移的操作。
     * @param list 集合
     * @param oldPosition 需要调换的元素
     * @param newPosition 被调换的元素
     * @param <T>
     */
    public static <T> List swap2(List<T> list, int oldPosition, int newPosition){
        if(null == list){
            throw new IllegalStateException("The list can not be empty...");
        }

        // 向前移动，前面的元素需要向后移动
        if(oldPosition < newPosition){
            for(int i = oldPosition; i < newPosition; i++){
                Collections.swap(list, i, i + 1);
            }
        }
        // 向后移动，后面的元素需要向前移动
        if(oldPosition > newPosition){
            for(int i = oldPosition; i > newPosition; i--){
                Collections.swap(list, i, i - 1);
            }
        }
        return list;
    }



    /**
     * 当前activity的名字 Service中无法使用
     *
     * @param context
     * @return
     */
    public static String getRunningActivityName(Context context) {
        String contextString = context.toString();
        return contextString.substring(contextString.lastIndexOf(".") + 1, contextString.indexOf("@"));
    }

}
